import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 于金鹏
 * @Date 2024/12/11 15:20
 * @Package yujinpeng
 */
public class ServletHomework {
    public static List<Student> getData() throws ClassNotFoundException {
        Class.forName("com.mysql.cj.jdbc.Driver");
        List<Student> students = new ArrayList<>();
        // 连接数据库
        String url = "jdbc:mysql://localhost:3306/student_manager";
        // 用户名
        String username = "root";
        // 密码
        String password = "root";
        try (Connection conn = DriverManager.getConnection(url, username, password);
             Statement statement = conn.createStatement();
             ResultSet resultSet = statement.executeQuery("SELECT * FROM student")
        ) {
            while (resultSet.next()) {
                Student student = new Student();
                student.setId(resultSet.getInt("id"));
                student.setName(resultSet.getString("name"));
                student.setSex(resultSet.getInt("sex"));
                student.setAge(resultSet.getInt("age"));
                student.setPhone(resultSet.getString("phone"));
                students.add(student);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return students;
    }
}
